package com.centanet.framework.task;

import android.os.AsyncTask;

import com.centanet.framework.interfaces.ICompressResult;
import com.centanet.framework.utils.ImageUtil;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;

/**
 * 图片压缩任务
 */
public class ImageCompressTask extends AsyncTask<Void, Void, ArrayList<String>> {

    private final static String TAG = "ImageCompressTask";

    private final String dir;
    private final ArrayList<String> arrayList;
    private final int targetSize;
    private final ICompressResult compressResult;

    /**
     * @param dir            压缩保存目录
     * @param arrayList      图片路径合集
     * @param targetSize     目标大小 0：不压缩；300：最大300k；类推
     * @param compressResult 压缩结果回调
     */
    public ImageCompressTask(String dir, ArrayList<String> arrayList, int targetSize, ICompressResult compressResult) {
        this.dir = dir;
        this.arrayList = arrayList;
        this.targetSize = targetSize;
        this.compressResult = compressResult;
    }

    @Override
    protected ArrayList<String> doInBackground(Void... params) {
        final long startTimeStamp = System.currentTimeMillis();
        ArrayList<String> result = new ArrayList<>();
        for (String s : arrayList) {
            result.add(ImageUtil.compress(dir, s, targetSize));
        }
        Logger.t(TAG).d("压缩消耗时间 : %d", System.currentTimeMillis() - startTimeStamp);
        return result;
    }

    @Override
    protected void onPostExecute(ArrayList<String> strings) {
        super.onPostExecute(strings);
        compressResult.result(strings);
    }
}